import type { FormRules } from "element-plus";
import { reactive } from "vue";
import { $t } from "@/plugins/i18n";
import { isUrl } from "@/utils/tools";

/** 自定义表单规则校验 */
export const menuFormRules = reactive<FormRules>({
    "path": [
        {
            required: true,
            message: $t("placeholder.inputRoutePath"),
            trigger: "blur",
        },
    ],
    "meta.title": [
        {
            required: true,
            message: $t("placeholder.inputName"),
            trigger: "blur",
        },
    ],
    "meta.url": [
        {
            required: true,
            message: $t("placeholder.inputLinkAddress"),
            trigger: "blur",
        },
        {
            validator: (rule: any, value: any, callback: any): void => {
                if (isUrl(value)) {
                    callback();
                } else {
                    callback($t("placeholder.invalidLinkAddress"));
                }
            },
            trigger: "blur",
        },
    ],
    "meta.auth": [
        {
            required: true,
            message: $t("placeholder.inputPermissionCode"),
            trigger: "blur",
        },
    ],
});
